/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QFontMetrics>
#include <QShowEvent>
#include <QGraphicsTextItem>
#include "listview.h"
#include "infoview.h"

InfoView::InfoView(QWidget *listView, QWidget *parent)
 : QGraphicsView(parent),m_listView(listView)
{
	setScene(&m_scene);
	connect(listView, SIGNAL(aboutToShow()), SLOT(show()));
	connect(listView, SIGNAL(aboutToHide()), SLOT(hide()));
	hide();
	setParent(0,Qt::ToolTip);
	setFocusPolicy(Qt::NoFocus);
}

void InfoView::showEvent(QShowEvent * event)
{
	QGraphicsView::showEvent(event);
	setGeometry(m_listView->geometry().right(), m_listView->geometry().top(), geometry().width(), geometry().height());
	m_scene.clear();
}

void InfoView::setInfoFields(const QStringList & infoFields)
{
	m_infoFields=infoFields;
}
void InfoView::setInfoHeads(const QStringList & infoHeads)
{
	m_infoHeads=infoHeads;
}

void InfoView::currentRowChanged(const QModelIndex & current, const QModelIndex & /*previous*/)
{
	m_scene.clear();
	if (!current.isValid() || !current.model())
		return;
	int fh=QFontMetrics(m_scene.font()).height();
	int y=0;
	for (int i=0;i<m_infoHeads.size();i++)
	{
		QGraphicsTextItem * ti= new QGraphicsTextItem();
		ti->setPos(0,y);
		QModelIndex idx=current.model()->index(current.row(), i+1, current.parent());
		if (idx.isValid())
			ti->setHtml(QString("<i>%1</i>: <b>%2</b>").arg(m_infoHeads[i]).arg(current.model()->data(idx).toString()));
		m_scene.addItem(ti);
		y+=fh;
	}
}

void InfoView::setSelectionModel(QItemSelectionModel * ism)
{
	connect(ism, SIGNAL(currentRowChanged(const QModelIndex&, const QModelIndex&)), SLOT(currentRowChanged(const QModelIndex&, const QModelIndex&)));
}

